import _ from 'lodash';

// 组织树加最外层节点
export const addParentElement = list => {
  const orgshortname = list[0].orgnamefullpath.split('-')[0];
  const tree = [];
  tree.push({
    orgdepid: list[0].parentOrgid,
    orgshortname,
    orgnamefullpath: orgshortname,
    orgnofullpath: list[0].parentOrgid,
    orgtype: 'E0',
    parentOrgid: '',
    childMenu: list,
  });
  return tree;
};

// 循环组织树
export const orgBluidTree = list => {
  const parentKey = [];
  list.map(item => {
    const obj = {
      key: item.orgdepid,
      value: item.orgdepid,
      title: item.orgshortname || item.orgname,
      children:
        item.childMenu || item.children ? orgBluidTree(item.childMenu || item.children) : [],
    };
    parentKey.push(obj);
    return parentKey;
  });
  return parentKey;
};
// 筛选出指定的数据
export const buildTree = (list, searchValue) => {
  const newTreeArr = [];
  const expandedKeys = [];
  list.map(item => {
    if (item.childMenu && item.childMenu.length) {
      const x = buildTree(item.childMenu, searchValue);
      const obj = {
        ...item,
        childMenu: x.newTreeArr,
      };
      const arr = [...x.expandedKeys];
      if (x.newTreeArr && x.newTreeArr.length) {
        expandedKeys.push(arr);
        newTreeArr.push(obj);
      }
    } else if (
      item.orgshortname ? item.orgshortname.indexOf(searchValue) > -1 : item.orgshortname
    ) {
      expandedKeys.push(item.orgdepid);
      newTreeArr.push(item);
    }
    return { newTreeArr, expandedKeys };
  });
  return { newTreeArr, expandedKeys };
};
// 加key值
export const buildPids = (list, pnode = null) => {
  const tree = [];
  list.forEach(value => {
    const x = value;
    x.pids = pnode ? _.union(pnode.pids, [pnode.orgdepid]) : [];
    tree.push({
      key: x.orgdepid,
      title: x.orgname,
      flag: x.flag,
      pids: x.pids,
      parentOrgid: x.parentOrgid,
      children: x.childMenu && x.childMenu.length > 0 ? buildPids(x.childMenu, x) : [],
    });
  });
  return tree;
};
// 返回半选状态
export const getHalfChecked = (list, ids) => {
  let halfChecked = [];
  list.forEach(value => {
    const x = value;
    if (ids.indexOf(value.key) > -1) {
      halfChecked = _.union(halfChecked, value.pids);
    }
    if (x.children && x.children.length > 0) {
      halfChecked = _.union(halfChecked, getHalfChecked(x.children, ids));
    }
  });
  return halfChecked;
};
// 返回全选
export const getCheckedKeys = (list, ids) => {
  list.forEach(x => {
    if (x.flag === '1') {
      ids.push(x.key);
    }
    if (x.children && x.children.length > 0) {
      getCheckedKeys(x.children, ids);
    }
  });
  return ids;
};

export const loopTree = (tree, lis) => {
  _.forEach(tree, x => {
    lis.push(x);
    if (x.children && x.children.length > 0) {
      loopTree(x.children, lis);
    }
  });
  return lis;
};

// 获取后台需要的checkedId
export const getParamsCheckedIds = (tree, checkedKeys) => {
  const ids = [];
  const list = loopTree(tree, []);
  let checkedItem = _.filter(list, item => _.indexOf(checkedKeys, item.key) > -1);
  checkedItem = _.filter(checkedItem, item => _.indexOf(checkedKeys, item.parentOrgid) <= -1);
  _.forEach(checkedItem, item => {
    ids.push(item.key);
  });
  return ids;
};

// 功能权限-组成树
export const translateDataToTree = (list, pid) => {
  const root = _.filter(list, node => {
    if (node.parentCode === pid && node.perType * 1 !== 3) {
      return true;
    }
    return false;
  });

  if (root && root.length > 0) {
    root.forEach(element => {
      const x = element;
      const btns = _.filter(list, node => {
        if (node.parentCode === x.perCode && node.perType * 1 === 3) {
          return true;
        }
        return false;
      });
      x.btns = btns;
      x.children = translateDataToTree(list, x.perCode);
    });
  }
  return root;
};

// 找出选中得子节点
export const findNode = (items, key) => {
  let x = null;
  if (items && items.length > 0) {
    items.forEach(element => {
      if (key === element.key) {
        x = element;
      } else {
        const found = findNode(element.children, key);
        if (found) {
          x = found;
        }
      }
    });
  }
  return x;
};

// 返回对象里的一个属性
export const getArrayProps = (array, key) => {
  const res = [];
  if (array) {
    array.forEach(t => {
      res.push(t[key]);
    });
  }
  return res;
};
